<?php

namespace app\index\controller;

use BaiduMiniProgram\Payment\PaymentClient;
use think\facade\Config;

class Pay
{
	public function gen()
	{

		$config = Config::pull("pay");
		$clinet = new  PaymentClient($config['dealId'],
			$config['appKey'],
			$config['privateKey'],
			$config['publicKey']);

		$orderId = $this->genOrderId();
		$totalAmount = 1;
		$rsaSign = $clinet->signForPolymerPayment($orderId, $totalAmount);
		$res = [
			'code' => 0,
			'msg' => ''
		];
		$res['data'] = [
			'appKey' => $config['appKey'],
			'dealId' => $config['dealId'],
			'dealTitle' => "订单标题",
			'rsaSign' => $rsaSign,
			'totalAmount' => $totalAmount,
			'tpOrderId' => $orderId,
			'bizInfo' => ''
		];
		return $res;
	}

	private function genOrderId()
	{
		return date("Ymdhis") . random_int(0, 99999);
	}
}